package org.jsola.contract.web;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.VariableAddDTO;
import org.jsola.contract.dto.VariableUpdateDTO;
import org.jsola.contract.query.VariableQuery;
import org.jsola.contract.service.IVariableService;
import org.jsola.contract.vo.VariableListVO;
import org.jsola.contract.vo.VariableVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 模板变量
 *
 * @author JanusMix
 */
@RestController("contractVariableController")
@RequestMapping(ContractConstants.API_PATH + "/v1/variables")
public class VariableController implements IVariableControllerAPI {

    @Autowired
    private IVariableService variableService;

    @Override
    @PostMapping
    public Result<VariableVO> save(@Valid @RequestBody VariableAddDTO variableAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增模板变量权限校验

        return Result.success(
                variableService.save(variableAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody VariableUpdateDTO variableUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改模板变量权限校验

        int count = variableService.update(variableUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{variableId}")
    public Result<Integer> delete(@PathVariable Long variableId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除模板变量权限校验

        int count = variableService.deleteByIds(tokenUser, variableId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] variableIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除模板变量权限校验

        int count = variableService.deleteByIds(tokenUser, variableIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{variableId}")
    public Result<VariableVO> findById(@PathVariable Long variableId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看模板变量权限校验

        return Result.success(
            variableService.selectById(variableId)
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<VariableListVO>> listAll(@Valid VariableQuery variableQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看模板变量权限校验

        return Result.success(
            variableService.select(variableQuery)
        );
    }


    @Override
    @GetMapping
    public Result<Page<VariableListVO>> page(@Valid VariableQuery variableQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            variableService.selectPage(variableQuery)
        );
    }


}

